using MediatR;
using Microsoft.EntityFrameworkCore;
using RBAC.Domain.SmartHealthcare.Hosiptial;
using RBAC.ErrorCode;
using RBAC.Interstructrue.Base;
using SmartHealthcare.Read.Api.Application.Command.Medicine;
using SmartHealthcare.Read.Api.DTo;

namespace SmartHealthcare.Read.Api.Application.Handler.Medicine
{
    public class GetMedicineStorageHandler : IRequestHandler<GetMedicineStorageCommand, APIResult<List<MedicineStorageDto>>>
    {
        private readonly IBaseRepository<MedicineModel> medicineRepository;
        private readonly ILogger<GetMedicineStorageHandler> logger;

        public GetMedicineStorageHandler(IBaseRepository<MedicineModel> medicineRepository,
            ILogger<GetMedicineStorageHandler> logger)
        {
            this.medicineRepository = medicineRepository;
            this.logger = logger;
        }

        public async Task<APIResult<List<MedicineStorageDto>>> Handle(GetMedicineStorageCommand request, CancellationToken cancellationToken)
        {
            var result = new APIResult<List<MedicineStorageDto>>();
            try
            {
                var query = medicineRepository.GetALL().Where(x => x.IsDeleted == false);

                if (!string.IsNullOrWhiteSpace(request.MedicineName))
                    query = query.Where(x => x.MedicineName.Contains(request.MedicineName));
                if (!string.IsNullOrWhiteSpace(request.Specification))
                    query = query.Where(x => x.Specification.Contains(request.Specification));
                if (!string.IsNullOrWhiteSpace(request.DosageForm))
                    query = query.Where(x => x.DosageForm.Contains(request.DosageForm));
                if (!string.IsNullOrWhiteSpace(request.DrugType))
                    query = query.Where(x => x.DrugType.Contains(request.DrugType));
                if (!string.IsNullOrWhiteSpace(request.CostName))
                    query = query.Where(x => x.CostName.Contains(request.CostName));
                if (request.Inventory.HasValue)
                    query = query.Where(x => x.Inventory == request.Inventory.Value);
                if (request.InventoryLimit.HasValue)
                    query = query.Where(x => x.InventoryLimit == request.InventoryLimit.Value);
                if (request.InventoryLowerLimit.HasValue)
                    query = query.Where(x => x.InventoryLowerLimit == request.InventoryLowerLimit.Value);
                if (request.Price.HasValue)
                    query = query.Where(x => x.Price == request.Price.Value);
                if (request.PurchasePrice.HasValue)
                    query = query.Where(x => x.PurchasePrice == request.PurchasePrice.Value);
                if (request.ManufactureDateFrom.HasValue)
                    query = query.Where(x => x.Date_ofManufacture >= request.ManufactureDateFrom.Value);
                if (request.ManufactureDateTo.HasValue)
                    query = query.Where(x => x.Date_ofManufacture <= request.ManufactureDateTo.Value);
                if (request.EffectiveDateFrom.HasValue)
                    query = query.Where(x => x.EffectiveDate >= request.EffectiveDateFrom.Value);
                if (request.EffectiveDateTo.HasValue)
                    query = query.Where(x => x.EffectiveDate <= request.EffectiveDateTo.Value);
                if (!string.IsNullOrWhiteSpace(request.Effect))
                    query = query.Where(x => x.Effect.Contains(request.Effect));

                var dto = await query
                    .OrderByDescending(x => x.CreateTime)
                    .Select(x => new MedicineStorageDto
                    {
                        Id = x.Id,
                        MedicineName = x.MedicineName,
                        Specification = x.Specification,
                        DosageForm = x.DosageForm,
                        DrugType = x.DrugType,
                        CostName = x.CostName,
                        Inventory = x.Inventory,
                        InventoryLimit = x.InventoryLimit,
                        InventoryLowerLimit = x.InventoryLowerLimit,
                        Price = x.Price,
                        PurchasePrice = x.PurchasePrice,
                        Date_ofManufacture = x.Date_ofManufacture,
                        EffectiveDate = x.EffectiveDate,
                        Effect = x.Effect
                    })
                    .ToListAsync(cancellationToken);

                result.Data = dto;
                result.Code = APIEnums.Success;
                result.Msg = "查询成功";
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "查询药品入库失败");
                result.Code = APIEnums.Error;
                result.Msg = "查询失败：" + ex.Message;
            }

            return result;
        }
    }
}
